﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1.segments.field
{
    class CX
    {
        private StringBuilder ID;
        private StringBuilder check_digit;
        private StringBuilder check_digit_scheme;
        private HD assigning_authority;
        private StringBuilder identifier_type_code;
        private HD assigning_facility;

        public StringBuilder id { get { return ID; } set { ID = value; } }
        public StringBuilder check_Digit { get { return check_digit; } set { check_digit = value; } }
        public StringBuilder check_Digit_scheme { get { return check_digit_scheme; } set { check_digit_scheme = value; } }
        public HD assigning_Authority { get { return assigning_authority; } set { assigning_authority = value; } }
        public StringBuilder identifier_Type_code { get { return identifier_type_code; } set { identifier_type_code = value; } }
        public HD assigning_Facililty { get { return assigning_facility; } set { assigning_facility = value; } }

        public CX()
        {
            ID = new StringBuilder();
            check_digit = new StringBuilder();
            check_digit_scheme = new StringBuilder();
            assigning_authority = new HD();
            assigning_facility = new HD();
            identifier_type_code = new StringBuilder();
        }

        enum segments { DG1, EVN, MSH, OBX, ORC, PID, PV1, RXO, NTE, OBR }

        string check(string source, int temp)
        {

            if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.MSH.ToString())
                return segments.MSH.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.DG1.ToString())
                return segments.DG1.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.EVN.ToString())
                return segments.EVN.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.NTE.ToString())
                return segments.NTE.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.OBR.ToString())
                return segments.OBR.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.OBX.ToString())
                return segments.OBX.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.ORC.ToString())
                return segments.ORC.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.PID.ToString())
                return segments.PID.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.PV1.ToString())
                return segments.PV1.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.RXO.ToString())
                return segments.RXO.ToString();

            return null;
        }

        public int fill(string source, int temp)
        {
            int i, x = 0;
            for (i = temp; i < source.Length; i++)
            {
                if (check(source, i) != null)
                {
                    id.Capacity = id.Length;
                    check_digit.Capacity = check_digit.Length;
                    check_digit_scheme.Capacity = check_digit_scheme.Length;
                    identifier_type_code.Capacity = identifier_type_code.Length;
                    return i-1;
                }
                else if (source[i] == '^')
                    x++;
                else if (source[i] == '|')
                {
                    id.Capacity = id.Length;
                    check_digit.Capacity = check_digit.Length;
                    check_digit_scheme.Capacity = check_digit_scheme.Length;
                    identifier_type_code.Capacity = identifier_type_code.Length;
                    return i;
                }

                else if (x == 0)
                    id.Insert(id.Length, source[i]);
                else if (x == 1)
                    check_digit.Insert(check_digit.Length, source[i]);
                else if (x == 2)
                    check_digit_scheme.Insert(check_digit_scheme.Length, source[i]);
                else if (x == 3)
                    i = assigning_authority.fill(source, i);
                else if (x == 4)
                    identifier_type_code.Insert(identifier_type_code.Length, source[i]);
                else if (x == 5)
                    i = assigning_facility.fill(source, i); 
            }
            return i;
        }

        public override string ToString()
        {
            string CX_string = "";

            if (ID.Length != 0)
                CX_string += ID;
            if (check_digit.Length != 0)
                CX_string += "^" + check_digit;
            if (check_digit_scheme.Length != 0)
                CX_string += "^" + check_digit_scheme;
            if (assigning_authority != null)
                CX_string += "^" + assigning_authority.tostring();
            if (identifier_type_code.Length != 0)
                CX_string += "^" + identifier_type_code;
            if (assigning_facility != null)
                CX_string += "^" + assigning_facility.tostring();
           
            return CX_string;
        }
    }
}